package com.campus_information_portal.service.impl;

import com.campus_information_portal.dao.PartyActivityDao;
import com.campus_information_portal.entity.PartyActivity;
import com.campus_information_portal.entity.PartyActivityConditions;
import com.campus_information_portal.entity.PartyActivityTemp;
import com.campus_information_portal.entity.PartyMemberTemp;
import com.campus_information_portal.service.PartyActivityService;
import com.campus_information_portal.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 20155880 房雁颖
 * @description 党支部活动业务实现
 * @project_name PartyActivityImpl
 * @package com.campus_information_portal.service.impl
 * @create 2018-05-13 8:34
 **/
@Service
@Transactional(rollbackFor=Exception.class)
public class PartyActivityImpl implements PartyActivityService{
    @Autowired
    private PartyActivityDao partyActivityDao;

    /**
     * @param partyActivity
     * @Description 党支部活动录入
     * @author 20155880 房雁颖
     * @methodName addPartyActivity
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message addPartyActivity(PartyActivity partyActivity) {
        boolean flag =partyActivityDao.addPartyActivity(partyActivity,partyActivity.getPartyActivityId());
        if (flag==true){
            return Message.success();
        }
        return Message.fail().add("1107","增加失败");
    }

    /**
     * @param partyActivity
     * @Description 党支部活动修改，保存
     * @author 20155880 房雁颖
     * @methodName savePartyActivity
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message savePartyActivity(PartyActivity partyActivity) {
//        boolean coverFlag;
//        boolean saveFlag;
//        boolean saveTempFlag;
//        PartyActivityTemp partyActivityTemp=partyActivityDao.queryPartyActivityTemp(partyActivity.getPartyOrganizationNum());
//        //如果临时表无该活动记录，则增加一条临时记录；否则覆盖该活动的临时记录，同时修改活动表状态为1
//        if(partyActivityTemp!=null){
//            System.out.println("------临时表有记录-------");
//            coverFlag=partyActivityDao.coverPartyActivityTemp(partyActivity);
//            if(coverFlag==true){
//                return Message.success();
//            }
//        }else {
//            saveTempFlag =partyActivityDao.savePartyActivityTemp(partyActivity,(int)System.currentTimeMillis());
//            saveFlag=partyActivityDao.savePartyActivity(partyActivity);
//            if((saveFlag==true)&&(saveTempFlag==true)){
//                return Message.success();
//            }
//        }
       boolean saveFlag=partyActivityDao.savePartyActivity(partyActivity);
       if (saveFlag==true){
           return Message.success();
       }
        return Message.fail().add("1108","保存失败");
    }

    /**
     * @param partyActivity
     * @Description 党支部活动修改，提交
     * @author 20155880 房雁颖
     * @methodName commitPartyActivity
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message commitPartyActivity(PartyActivity partyActivity) {
//        boolean flag1=partyActivityDao.deletePartyActivityTemp(partyActivity.getPartyActivityId());
//        boolean flag2 =partyActivityDao.commitPartyActivity(partyActivity);
//
//        if ((flag1==true)&&(flag2==true)){
//            return Message.success();
//        }
        boolean flag =partyActivityDao.commitPartyActivity(partyActivity);
        if (flag==true){
            return Message.success();
        }
        return Message.fail().add("1109","提交失败");
    }

    /**
     * @param party_activity_id
     * @Description 党支部活动删除
     * @author 20155880 房雁颖
     * @methodName deletePartyActivity
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message deletePartyActivity(int party_activity_id) {
        boolean flag =partyActivityDao.deletePartyActivity(party_activity_id);
        if (flag==true){
            return Message.success();
        }
        return Message.fail().add("1109","删除失败");
    }

    /**
     * @param partyActivityConditions
     * @Description 党支部活动动态查询(按活动名称、活动时间)，活动表
     * @author 20155880 房雁颖
     * @methodName queryPartyActivityByConditions
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message queryPartyActivityByConditions(PartyActivityConditions partyActivityConditions) {
       List<PartyActivity> partyActivityList=partyActivityDao.queryPartyActivityByConditions(partyActivityConditions);
        if (partyActivityList!=null){
            return Message.success().add("partyActivityList",partyActivityList);
        }
        return Message.fail().add("1110","查询失败");
    }

    /**
     * @param partyActivityConditions
     * @Description 党支部活动动态查询(按活动名称、活动时间)，活动表
     * @author 20155880 房雁颖
     * @methodName queryPartyActivityByConditionsByCache
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Cacheable(value = "myCache", key = "'PartyActivityService.queryPartyActivityByConditionsByCache'")
    @Override
    public Message queryPartyActivityByConditionsByCache(PartyActivityConditions partyActivityConditions) {
        System.out.println("*****************************缓存开启***************************************");
        List<PartyActivity> partyActivityList=partyActivityDao.queryPartyActivityByConditionsByCache(partyActivityConditions);
        if (partyActivityList!=null){
            return Message.success().add("partyActivityList",partyActivityList);
        }
        return Message.fail().add("1110","查询失败");
    }


//    /**
//     * @param partyActivityConditions
//     * @Description 党支部活动动态查询(按活动名称、活动时间)，临时表
//     * @author 20155880 房雁颖
//     * @methodName queryPartyActivityTempByConditions
//     * @returnType Message
//     * @create 2018/4/26 8:45
//     **/
//    @Override
//    public Message queryPartyActivityTempByConditions(PartyActivityConditions partyActivityConditions) {
//        List<PartyActivityTemp> partyActivityTempList=partyActivityDao.queryPartyActivityTempByConditions(partyActivityConditions);
//        if (partyActivityTempList!=null){
//            return Message.success().add("partyActivityTempList",partyActivityTempList);
//        }
//        return Message.fail().add("1111","查询失败");
//    }


    /**
     * @param party_organization_num
     * @Description 党支部活动统计，通过党支部编号
     * @author 20155880 房雁颖
     * @methodName countActivity
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message countActivity(int party_organization_num) {
        int countActivity=partyActivityDao.countActivity(party_organization_num);
        if (countActivity!=0){
            return Message.success().add("countActivity",countActivity);
        }
        return Message.fail().add("1111","查询失败");
    }

    /**
     * @param party_activity_id
     * @Description 通过党支部活动ID查找党支部活动信息,活动表
     * @author 20155880 房雁颖
     * @methodName queryPartyActivityById
     * @returnType PartyActivity
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message queryPartyActivityById(int party_activity_id) {
        PartyActivity partyActivity=partyActivityDao.queryPartyActivityById(party_activity_id);
        if (partyActivity!=null){
            return Message.success().add("partyActivity",partyActivity);
        }
        return Message.fail().add("1112","查询失败");
    }

//    /**
//     * @param party_activity_id
//     * @Description 通过党支部活动ID查找党支部活动信息,临时表
//     * @author 20155880 房雁颖
//     * @methodName queryPartyActivityTempById
//     * @returnType partyActivityTemp
//     * @create 2018/4/26 8:45
//     **/
//    @Override
//    public Message queryPartyActivityTempById(int party_activity_id) {
//        PartyActivityTemp partyActivityTemp=partyActivityDao.queryPartyActivityTempById(party_activity_id);
//        if (partyActivityTemp!=null){
//            return Message.success().add("partyActivityTemp",partyActivityTemp);
//        }
//        return Message.fail().add("1113","查询失败");
//    }


    /**
     * @param party_activity_id
     * @Description 通过党支部活动ID查找党支部名称
     * @author 20155880 房雁颖
     * @methodName queryPartyNameByPAI
     * @returnType Message
     * @create 2018/4/26 8:45
     **/
    @Override
    public Message queryPartyNameByPAI(int party_activity_id) {
        String partyName=partyActivityDao.queryPartyNameByPAI(party_activity_id);
        if(partyName!=null){
            return Message.success().add("partyName",partyName);
        }
        return Message.fail().add("1113","查询失败");
    }
}
